package com.hspedu.outputStream;

import java.io.*;

/**
 * @author LiuJinTao
 * @className ObjectOutStream_
 * @date 2024/06/28 10:57
 * @description TODO
 */
public class ObjectOutStream_ {
    public static void main(String[] args) throws IOException {
        String path = "e:\\data.dat";

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));

        // 序列化数据
        oos.writeInt(100);// int -> Integer(实现了 serializable)
        oos.writeBoolean(true);// boolean -> Boolean(实现了 serializable)
        oos.writeChar('a');// char -> Character(实现了 serializable)
        oos.writeDouble(9.7);// double -> Double(实现了 serializable)
        oos.writeUTF("刘金涛");// String
        oos.writeObject(new Dog(1, "xiaohei"));

        oos.close();
        System.out.println("序列化完毕");
    }
}

class Dog implements Serializable {
    private int age;
    private String name;

    public Dog(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}